<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { getOrderAPI } from '../../apis/pay'
import { useCountDown } from '@/composables/useCountDown'
import { useCartStore } from '@/stores/cart'

const cartStore = useCartStore()

const { formatTime, start } = useCountDown()
const payInfo = ref({})
const route = useRoute()
const loading = ref(true)

const getOrder = async () => {
  loading.value = true
  try {
    const res = await getOrderAPI(route.query.id)
    payInfo.value = res.result

    // 初始化倒计时
    start(res.result.countdown)
  } catch (error) {
    console.error('获取订单信息失败:', error)
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  getOrder()
  cartStore.updateCart()
})

// 支付地址
const baseURL = 'http://pcapi-xiaotuxian-front-devtest.itheima.net/'
const backURL = 'http://localhost:5173/paycallback'
const redirectUrl = encodeURIComponent(backURL)
const payUrl = `${baseURL}pay/aliPay?orderId=${route.query.id}&redirect=${redirectUrl}`
</script>

<template>
  <div class="xtx-pay-page">
    <div class="container" v-loading="loading" element-loading-text="正在加载支付信息...">
      <!-- 付款信息 -->
        <div class="pay-info">
          <span class="icon iconfont icon-queren2"></span>
          <div class="tip">
            <p>订单提交成功！请尽快完成支付。</p>
            <p>支付还剩 <span>{{ formatTime }}</span>, 超时后将取消订单</p>
          </div>
          <div class="amount">
            <span>应付总额：</span>
            <span>¥{{ payInfo.payMoney?.toFixed(2) }}</span>
          </div>
        </div>
        <!-- 付款方式 -->
        <div class="pay-type">
          <p class="head">选择以下支付方式付款</p>
          <div class="item">
            <p>支付平台</p>
            <a class="btn wx disabled" href="javascript:;" disabled></a>
            <a class="btn alipay" :href="payUrl"></a>
          </div>
          <div class="item">
            <p>支付方式</p>
            <a class="btn disabled" href="javascript:;" disabled>招商银行</a>
            <a class="btn disabled" href="javascript:;" disabled>工商银行</a>
            <a class="btn disabled" href="javascript:;" disabled>建设银行</a>
            <a class="btn disabled" href="javascript:;" disabled>农业银行</a>
            <a class="btn disabled" href="javascript:;" disabled>交通银行</a>
          </div>
        </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.xtx-pay-page {
  margin-top: 20px;
  min-height: 80vh;
  display: flex;
  align-items: center;
}

.container {
  min-height: 600px;
  // 确保Element Plus的loading组件能正确显示
  position: relative;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.pay-info {
  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;

  .icon {
    font-size: 80px;
    color: #1dc779;
  }

  .tip {
    padding-left: 10px;
    flex: 1;

    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }

      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }

  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }

      &:last-child {
        color: $priceColor;
        font-size: 20px;
      }
    }
  }
}

.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;

  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;

    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }

  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;

    &.active,
    &:hover {
        border-color: $xtxColor;
      }

      &.disabled {
        opacity: 0.6;
        cursor: not-allowed;
        pointer-events: none;

        &:hover {
          border-color: #e4e4e4;
        }
      }

    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png) no-repeat center / contain;
    }

    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg) no-repeat center / contain;
    }
  }
}
</style>
